/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__receive_data.c                                  */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/12 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "../../comm/frcmd_parse_detail.h"
#include "../../comm/client.pb-c.h"
#include "../../comm/frcmd_parse.h"
#include "../../comm/apm.pb-c.h"
#include "../../comm/frcmd_client.h"
#include "../../comm/frcomm.h"
#include "../../comm/frmem.h"
#include "../../comm/frsutl.h"
#include "../../comm/apmtest_cunit.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

extern int g_i_port;
extern char* g_p_server;

int __send_data(int i_sockfd, char *p_cmd, unsigned int ui_cmd_len);
int __receive_data(int i_sockfd);
int __init_socket(const char *p_addr, unsigned short us_port, int *p_sockfd);
int __protobuf_do_pack(Command *p_st_cmd, char **pp_data, unsigned int *p_ui_out);

void case1(void)
{

    int i_ret = 0;
    Command *p_st_cmd = NULL;
    char *p_data = NULL;
    unsigned int ui_out = 0;
    L7Command *l7_cmd = NULL;
    L7Setting *l7_settings = NULL;
    int i_sockfd = 5;
    //设置l7_settings
    l7_settings = frmalloc(sizeof(L7Setting));
    l7_setting__init(l7_settings);
    l7_settings->has_ip_proto = 1;
    l7_settings->ip_proto = 1;
    l7_settings->has_ip = 1;
    l7_settings->ip = iptools_parse_ipaddr("192.168.1.1");
    l7_settings->port = "6535";
    l7_settings->l7_proto = frmalloc(2);
    snprintf(l7_settings->l7_proto,2,"%s","0");

    //设置l7_cmd
    l7_cmd = frmalloc(sizeof(L7Command));
    l7_command__init(l7_cmd);
    l7_cmd->action = frmalloc(strlen("add") + 1);
    snprintf(l7_cmd->action,strlen("add") + 1,"%s","add");
    l7_cmd->n_l7_settings = 1;
    l7_cmd->l7_settings = &l7_settings;

    //设置p_st_cmd
    p_st_cmd = frmalloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->l7_cmd = l7_cmd;
    p_st_cmd->cmd_type = 1;

    i_ret = __protobuf_do_pack(p_st_cmd,&p_data,&ui_out);


    g_i_port = 6555;
    g_p_server = "127.0.0.1";

    i_ret = __init_socket(g_p_server, g_i_port, &i_sockfd);
    if(i_ret != FR_SUCC)
        return;

    i_ret = __send_data(i_sockfd, p_data, ui_out);
    if(i_ret != FR_SUCC)
        return;

    i_ret = __receive_data(i_sockfd);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(l7_settings->l7_proto);
    frfree(l7_settings);
    frfree(l7_cmd->action);
    frfree(l7_cmd);
    frfree(p_st_cmd);
    frfree(p_data);


}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__receive_data 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);

   //	CU_add_test(pSuite, "case2", case2);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(10240,"test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}